package com.gwsdk.paysdk.acitvityV2;

import android.annotation.SuppressLint;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwsdk.open.AuthenticationResult;
import com.gwsdk.open.GWApi;
import com.gwsdk.paysdk.acitvityV2.fragment.GWCouponFragment;
import com.gwsdk.paysdk.acitvityV2.fragment.GWGameGiftFragment;
import com.gwsdk.paysdk.acitvityV2.fragment.GWGameNoticeFragment;
import com.gwsdk.paysdk.acitvityV2.fragment.GWUserInfoFragment;
import com.gwsdk.paysdk.bean.AntiAddictionModel;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.entity.AntiAddiction;
import com.gwsdk.paysdk.entity.ChannelAndGameinfo;
import com.gwsdk.paysdk.entity.UserLogin;
import com.gwsdk.paysdk.http.process.UserInfoProcess;
import com.gwsdk.paysdk.listener.OnMultiClickListener;
import com.gwsdk.paysdk.manager.GWGameManager;
import com.gwsdk.paysdk.utils.AppStatus;
import com.gwsdk.paysdk.utils.AppStatusManager;
import com.gwsdk.paysdk.utils.AppUtils;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.GWLog;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：功能弹窗
 * 作者：钮家齐
 * 时间: 2018-09-08 11:11
 */

public class GWMainMenuActivity extends Activity {

    private View view_null;
    private View parentView;
    private LinearLayout mainMenuView;

    private FrameLayout frameLayout;
    private FragmentManager fragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private List<Fragment> fragments = new ArrayList<>();
    private List<Fragment> otherFragments = new ArrayList<>();

    private LinearLayout userInfoLayout;
    private LinearLayout gameGiftLayout;
    private LinearLayout couponLayout;
    private LinearLayout gameLayout;

    private GWUserInfoFragment userInfoFragment;
    private GWGameGiftFragment giftFragment;
    private GWGameNoticeFragment noticeFragment;
    private GWCouponFragment couponFragment;

    private TextView totalBadge;

    private List<View> itemViews = new ArrayList<>();
    private Fragment currentShowFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏(主要功能就是去除页面弹出时顶部黑色条)

        if(AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYVLE){
            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();
        }
        ComeOutType();
        super.onCreate(savedInstanceState);

        setContentView(GWInflaterUtils.getLayout(this,"gw_activity_main_menu"));


        mainMenuView = findViewById(GWInflaterUtils.getControl(this,"gw_main_menu"));
        parentView = findViewById(GWInflaterUtils.getControl(this,"parentView"));
        frameLayout = findViewById(GWInflaterUtils.getControl(this,"frame_layout"));
        view_null = findViewById(GWInflaterUtils.getControl(this,"view_gw_function_null"));

        userInfoLayout = findViewById(GWInflaterUtils.getControl(this,"gw_main_user_info"));
        gameGiftLayout = findViewById(GWInflaterUtils.getControl(this,"gw_main_game_gift"));
        couponLayout = findViewById(GWInflaterUtils.getControl(this,"gw_main_coupon"));
        gameLayout = findViewById(GWInflaterUtils.getControl(this,"gw_main_game"));
        totalBadge = findViewById(GWInflaterUtils.getControl(this,"total_badge"));
        fragmentManager = getFragmentManager();
        userInfoFragment = new GWUserInfoFragment();
        fragments.add(userInfoFragment);
        addFragment(userInfoFragment);
        setItemViewBackground(userInfoLayout);
        itemViews.add(userInfoLayout);
        itemViews.add(gameGiftLayout);
        itemViews.add(couponLayout);
        itemViews.add(gameLayout);


        addClick();

        quereUserInfo();
    }

    /**
     * 请求返回
     */
    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {

        }
    };


    private void quereUserInfo() {
        UserInfoProcess userInfoProcess = new UserInfoProcess();
        userInfoProcess.post(mHandler);
    }


    private void addClick(){

        view_null.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View view) {
                finish();
            }
        });


        userInfoLayout.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View view) {

                setItemViewBackground(view);
                if (userInfoFragment == null){
                    userInfoFragment = new GWUserInfoFragment();
                    fragments.add(userInfoFragment);
                }
                addFragment(userInfoFragment);
            }
        });

        gameGiftLayout.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View view) {
                setItemViewBackground(view);
                if (giftFragment == null){
                    giftFragment = new GWGameGiftFragment();
                    fragments.add(giftFragment);
                }
                addFragment(giftFragment);
            }
        });

        couponLayout.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View view) {
                setItemViewBackground(view);
                if (couponFragment == null){
                    couponFragment = new GWCouponFragment(1);
                    fragments.add(couponFragment);
                }
                addFragment(couponFragment);
            }
        });

        gameLayout.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View view) {
                setItemViewBackground(view);
                if (noticeFragment == null){
                    noticeFragment = new GWGameNoticeFragment();
                    fragments.add(noticeFragment);
                }
                addFragment(noticeFragment);
            }
        });
    }


    private void setItemViewBackground(View view){

        for (View item : itemViews){
            item.setBackground(null);
        }
        view.setBackgroundResource(GWInflaterUtils.getDrawable(this,"gw_shape_main_menu_selected_bg"));//getResources().getDrawable(R.drawable.));
    }


    @SuppressLint("ResourceType")
    private void addFragment(Fragment fragment){

        if (fragment == null) return;
        if (currentShowFragment == fragment) return;
        currentShowFragment = fragment;
        mFragmentTransaction = fragmentManager.beginTransaction();
        mFragmentTransaction.setCustomAnimations(GWInflaterUtils.getIdByName(this,"anim","gw_fragment_fade_in"),
                GWInflaterUtils.getIdByName(this,"anim","gw_fragment_fade_out"));
        hideFragment(mFragmentTransaction);
        if (fragment.isAdded()){
            fragment.onResume();
            mFragmentTransaction.show(fragment);
        }else {
            mFragmentTransaction.add(GWInflaterUtils.getControl(this,"frame_layout"),fragment,fragment.getClass().toString());
            mFragmentTransaction.show(fragment);
        }
        clearOtherFragment(mFragmentTransaction);
        mFragmentTransaction.commitAllowingStateLoss();

    }

    private void hideFragment(FragmentTransaction transaction){
        for(Fragment fragment : fragments){
            transaction.hide(fragment);
        }
    }

    @SuppressLint("ResourceType")
    public void addOtherFragment(Fragment fragment){
        mFragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(mFragmentTransaction);
        mFragmentTransaction.add(GWInflaterUtils.getControl(this,"frame_layout"),fragment,fragment.getClass().toString());
        mFragmentTransaction.show(fragment);
        mFragmentTransaction.commitAllowingStateLoss();
        otherFragments.add(fragment);
    }

    public void hideOtherFragment(Fragment fragment){
        mFragmentTransaction = fragmentManager.beginTransaction();
        otherFragments.remove(fragment);
        mFragmentTransaction.hide(fragment);
        mFragmentTransaction.remove(fragment);
        mFragmentTransaction.show(currentShowFragment);
        mFragmentTransaction.commitAllowingStateLoss();
        currentShowFragment.onResume();
    }

    private void clearOtherFragment(FragmentTransaction fragmentTransaction){
        for(Fragment fragment : otherFragments){
            fragmentTransaction.hide(fragment);
            fragmentTransaction.remove(fragment);
        }
        otherFragments.clear();
    }



    @SuppressLint("ResourceType")
    public void popFragment(Fragment fragment){
        mFragmentTransaction = fragmentManager.beginTransaction();
        mFragmentTransaction.setCustomAnimations(GWInflaterUtils.getIdByName(this,"anim","gw_fragment_fade_in"),
                GWInflaterUtils.getIdByName(this,"anim","gw_fragment_fade_out"));
        mFragmentTransaction.hide(fragment);
        mFragmentTransaction.remove(fragment);
        otherFragments.remove(fragment);
        mFragmentTransaction.show(currentShowFragment);
        mFragmentTransaction.commitAllowingStateLoss();
        currentShowFragment.onResume();
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
        int ori = mConfiguration.orientation; //获取屏幕方向
        int width = parentView.getWidth();
        if(ori == mConfiguration.ORIENTATION_LANDSCAPE){

            ViewGroup.LayoutParams layoutParams = mainMenuView.getLayoutParams();
            layoutParams.width = (width / 10 ) * 7;
            mainMenuView.setLayoutParams(layoutParams);
        }else{
            ViewGroup.LayoutParams layoutParams = mainMenuView.getLayoutParams();
            layoutParams.width = width;
            mainMenuView.setLayoutParams(layoutParams);
        }
    }

    private void initData() {

    }


    /**
     * 弹窗背景设置
     * 横竖屏弹出动画不同
     */
    private void ComeOutType() {
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        window.setDimAmount(0f);    //设置背景透明度   0~1
        // 判断Android当前的屏幕是横屏还是竖屏。横竖屏判断
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            //竖屏
            overridePendingTransition(GWInflaterUtils.getIdByName(this, "anim", "gw_push_bottom_in")
                    , 0);
        } else {
            //横屏
            overridePendingTransition(GWInflaterUtils.getIdByName(this, "anim", "gw_push_left_in")
                    ,0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void finish() {
        super.finish();

        //注释掉activity本身的过渡动画
        //判断Android当前的屏幕是横屏还是竖屏。横竖屏判断
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            //竖屏
            overridePendingTransition(0
                    , GWInflaterUtils.getIdByName(this, "anim", "gw_push_buttom_out"));
        } else {
            //横屏
            overridePendingTransition(0
                    , GWInflaterUtils.getIdByName(this, "anim", "gw_push_left_out"));
        }
    }


}
